package com.ruoyi.web.controller.module.survey;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.survey.Survey;
import com.ruoyi.system.domain.module.survey.SurveyQuestion;
import com.ruoyi.system.domain.module.survey.SurveyUserRecord;
import com.ruoyi.system.service.module.survey.ISurveyService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.survey.ISurveyQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 问卷调查问题Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Validated
@Api(value = "问卷调查问题控制器", tags = {"问卷调查问题管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/survey/question")
public class SurveyQuestionController extends BaseController {

    private final ISurveyService surveyService;
    private final ISurveyQuestionService surveyQuestionService;

    /**
     * 查询问卷调查问题列表
     */
    @ApiOperation("查询问卷调查问题列表")
    @PreAuthorize("@ss.hasPermi('module:question:list')")
    @GetMapping("/list")
    public TableDataInfo<SurveyQuestion> list(@Validated SurveyQuestion question) {
        QueryWrapper<SurveyQuestion> qw = new QueryWrapper<>();
        if(question.getSurveyId()!=null){
            qw.eq("survey_id",question.getSurveyId());
        }
        qw.orderByAsc("sort");
        return PageUtils.buildDataInfo(surveyQuestionService.page(PageUtils.buildPage(),qw));
    }

    /**
     * 导出问卷调查问题列表
     */
    @ApiOperation("导出问卷调查问题列表")
    @PreAuthorize("@ss.hasPermi('module:question:export')")
    @Log(title = "问卷调查问题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SurveyQuestion> export(@Validated SurveyQuestion surveyQuestion) {
		QueryWrapper<SurveyQuestion> qw = new QueryWrapper<>();
		if(surveyQuestion.getSurveyId()!=null){
			qw.eq("survey_id",surveyQuestion.getSurveyId());
		}
		qw.orderByAsc("sort");
        List<SurveyQuestion> list = surveyQuestionService.list(qw);
		if (list.size()>0){
			for (SurveyQuestion list1:list){
				if (list1.getQuestionType()!=null){
					if (list1.getQuestionType()==0){
						list1.setQuestionTypeString("选择");
					}
					if (list1.getQuestionType()==1){
						list1.setQuestionTypeString("输入");
					}
					if (list1.getQuestionType()==2){
						list1.setQuestionTypeString("图片");
					}
				}
			}
		}
        ExcelUtil<SurveyQuestion> util = new ExcelUtil<>(SurveyQuestion.class);
        return util.exportExcel(list, "问卷调查问题");
    }

    /**
     * 获取问卷调查问题详细信息
     */
    @ApiOperation("获取问卷调查问题详细信息")
    @PreAuthorize("@ss.hasPermi('module:question:query')")
    @GetMapping("/{id}")
    public AjaxResult<SurveyQuestion> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        SurveyQuestion question = surveyQuestionService.getById(id);
        if(question!=null){
            if(question.getQuestionType() == 0){
                JSONArray jsonArray = JSONUtil.parseArray(question.getAnswerSection());
                question.setSections(jsonArray);
            }
        }
        return AjaxResult.success(question);
    }

    /**
     * 新增问卷调查问题
     */
    @ApiOperation("新增问卷调查问题")
    @PreAuthorize("@ss.hasPermi('module:question:add')")
    @Log(title = "问卷调查问题", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SurveyQuestion surveyQuestion) {
        Survey survey = surveyService.getById(surveyQuestion.getSurveyId());
        if(survey!=null){
            surveyQuestion.setPropertyId(survey.getPropertyId());
            surveyQuestion.setQuestionTitle(survey.getQuestionTitle());
        }
        if(surveyQuestion.getQuestionType() == 1){
            surveyQuestion.setAnswerSection(null);
        }
        return toAjax(surveyQuestionService.save(surveyQuestion) ? 1 : 0);
    }

    /**
     * 修改问卷调查问题
     */
    @ApiOperation("修改问卷调查问题")
    @PreAuthorize("@ss.hasPermi('module:question:edit')")
    @Log(title = "问卷调查问题", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SurveyQuestion surveyQuestion) {
        Survey survey = surveyService.getById(surveyQuestion.getSurveyId());
        if(survey!=null){
            surveyQuestion.setPropertyId(survey.getPropertyId());
            surveyQuestion.setQuestionTitle(survey.getQuestionTitle());
        }
        if(surveyQuestion.getQuestionType() == 1){
            surveyQuestion.setAnswerSection(null);
        }
        return toAjax(surveyQuestionService.updateById(surveyQuestion) ? 1 : 0);
    }

    /**
     * 删除问卷调查问题
     */
    @ApiOperation("删除问卷调查问题")
    @PreAuthorize("@ss.hasPermi('module:question:remove')")
    @Log(title = "问卷调查问题" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(surveyQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
